package org.deltaset.faces.component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import javax.faces.component.UIComponent;

class CachingUIComponentList implements List<UIComponent> {

    private final List<UIComponent> original;
    private final List<UIComponent> everGrowing;

    public CachingUIComponentList(final List<UIComponent> original) {
        this.original = original;
        this.everGrowing = new ArrayList<UIComponent>(original.size());
        Collections.copy(this.everGrowing, this.original);
    }

    public List<UIComponent> allChildren() {
        return Collections.unmodifiableList(this.everGrowing);
    }

    /////////////
    @Override
    public <T> T[] toArray(T[] a) {
        return original.toArray(a);
    }

    @Override
    public Object[] toArray() {
        return original.toArray();
    }

    @Override
    public List<UIComponent> subList(int fromIndex, int toIndex) {
        return original.subList(fromIndex, toIndex);
    }

    @Override
    public int size() {
        return original.size();
    }

    @Override
    public UIComponent set(int index, UIComponent element) {
        everGrowing.set(index, element);
        return original.set(index, element);
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return original.retainAll(c);
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return original.removeAll(c);
    }

    @Override
    public UIComponent remove(int index) {
        return original.remove(index);
    }

    @Override
    public boolean remove(Object o) {
        return original.remove(o);
    }

    @Override
    public ListIterator<UIComponent> listIterator(int index) {
        return original.listIterator(index);
    }

    @Override
    public ListIterator<UIComponent> listIterator() {
        return original.listIterator();
    }

    @Override
    public int lastIndexOf(Object o) {
        return original.lastIndexOf(o);
    }

    @Override
    public Iterator<UIComponent> iterator() {
        return original.iterator();
    }

    @Override
    public boolean isEmpty() {
        return original.isEmpty();
    }

    @Override
    public int indexOf(Object o) {
        return original.indexOf(o);
    }

    @Override
    public int hashCode() {
        return original.hashCode();
    }

    @Override
    public UIComponent get(int index) {
        return original.get(index);
    }

    @Override
    public boolean equals(Object o) {
        return original.equals(o);
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return original.containsAll(c);
    }

    @Override
    public boolean contains(Object o) {
        return original.contains(o);
    }

    @Override
    public void clear() {
        original.clear();
    }

    @Override
    public boolean addAll(int index, Collection<? extends UIComponent> c) {
        everGrowing.addAll(index, c);
        return original.addAll(index, c);
    }

    @Override
    public boolean addAll(Collection<? extends UIComponent> c) {
        everGrowing.addAll(c);
        return original.addAll(c);
    }

    @Override
    public void add(int index, UIComponent element) {
        everGrowing.add(index, element);
        original.add(index, element);
    }

    @Override
    public boolean add(UIComponent e) {
        everGrowing.add(e);
        return original.add(e);
    }
}
